package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MyMusicThread;
import com.neutech.maplestory.util.MypleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏的主入口文件
 */
public class MapleStoryClient extends MypleStoryFrame {
    // 远景
    public BG bgs = new BG(this,"BG");
    // 背景
    public Background bg = new Background(this,0,120,"bg");
    // 人物
    public Hero hero = new Hero(this,1295,200);
    // 传送门1
    public Portal portal1 = new Portal(this,100,475);
    // 传送门2
    public Portal portal2 = new Portal(this,1700,110);
    // 弓箭
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();
    // 小怪
    public List<Mob> mobs = new CopyOnWriteArrayList<>();
    // 道具包
    public ItemPackage itemPackage = new ItemPackage(this);
    // 道具
    public List<Item> items = new CopyOnWriteArrayList<>();
    // 伤害值
    public List<Power> powers = new CopyOnWriteArrayList<>();
    // 地面
    public List<Ground> grounds = new CopyOnWriteArrayList<>();
    {
        for (int i = 0; i < 3; i++) {
            Mob mob = new Mob(this,300 + (i * 100),646,5);
            mobs.add(mob);
        }
        for (int i = 0; i < 2; i++) {
            Mob mob = new Mob(this,1025 + (i * 100),646,5);
            mobs.add(mob);
        }
        for (int i = 0; i < 2; i++) {
            Mob mob = new Mob(this,680 + (i * 100),96,5);
            mobs.add(mob);
        }
        for (int i = 0; i < 3; i++) {
            Mob mob = new Mob(this,100 + (i * 100),284,5);
            mobs.add(mob);
        }

    }
    {
        Ground ground01 = new Ground(this,0,725,745,200);
        grounds.add(ground01);
        Ground ground02 = new Ground(this,740,680,70,245);
        grounds.add(ground02);
        Ground ground03 = new Ground(this,810,640,135,285);
        grounds.add(ground03);
        Ground ground04 = new Ground(this,945,680,70,245);
        grounds.add(ground04);
        Ground ground05 = new Ground(this,1015,725,400,200);
        grounds.add(ground05);
        Ground ground06 = new Ground(this,1410,680,70,245);
        grounds.add(ground06);
        Ground ground07 = new Ground(this,1480,635,70,285);
        grounds.add(ground07);
        Ground ground08 = new Ground(this,1550,595,400,325);
        grounds.add(ground08);
        Ground ground09 = new Ground(this,0,365,1080,20);
        grounds.add(ground09);
        Ground ground10 = new Ground(this,1290,360,535,20);
        grounds.add(ground10);
        Ground ground11 = new Ground(this,665,175,670,20);
        grounds.add(ground11);
        Ground ground12 = new Ground(this,945,510,410,20);
        grounds.add(ground12);
        Ground ground13 = new Ground(this,130,200,100,20);
        grounds.add(ground13);
        Ground ground14 = new Ground(this,230,195,180,20);
        grounds.add(ground14);
        Ground ground15 = new Ground(this,410,190,130,20);
        grounds.add(ground15);
        }

    @Override
    public void init() {
        super.init();
        // 添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    // 开关切换
                    itemPackage.live = !itemPackage.live;
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });
        // 播放背景音乐
        new MyMusicThread("bgm.mp3", true).start();

        // 添加鼠标监听器
        /*this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
            }
        });*/
    }

    @Override
    public void paint(Graphics g) {

        bgs.draw(g);

        bg.draw(g);

        Iterator<Ground> itGrounds = grounds.iterator();
        while (itGrounds.hasNext()){
            Ground ground = itGrounds.next();
            ground.draw(g);
        }
        hero.draw(g);
        /*hero.hit(mobs);*/
        portal1.draw(g);
        portal2.draw(g);
        /*arrow.draw(g);*/

        //使用 迭代器和CopyOnWriteArrayList 解决数组索引移动问题（图片闪烁线程非安全）
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
        // 线程非安全
        /*for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow = arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
        }*/

        //使用 迭代器和CopyOnWriteArrayList 解决数组索引移动问题（图片闪烁线程非安全）
        Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
            mob.hit(hero);
        }

        // 线程非安全
        /*for (int i = 0; i < mobs.size(); i++) {
            mobs.get(i).draw(g);
        }*/

        Iterator<Item> itItems = items.iterator();
        while (itItems.hasNext()){
            Item item = itItems.next();
            item.draw(g);
        }

        Iterator<Power> itPowers = powers.iterator();
        while (itPowers.hasNext()){
            Power power = itPowers.next();
            power.draw(g);
        }

        /*Iterator<Portal> itPortals = portals.iterator();
        while (itPortals.hasNext()){
            Portal portals = itPortals.next();
            portals.draw(g);
        }*/


        itemPackage.draw(g);

        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("人物的方向为：" + hero.dir,100,100);
        g.drawString("人物的动作为：" + hero.action,100,150);
        g.drawString("人物的HP为：" + hero.HP,100,200);
        g.drawString("人物的MP为：" + hero.MP,100,250);
        g.drawString("人物的等级为：" + hero.LEVEL,100,300);
        g.setFont(f);
    }

    public static void main(String[] args) {
        new MapleStoryClient().init();
    }
}
